package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-05-30 15:14
 */
public class Test29 {
    public static void main(String[] args) {

    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    private ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode p = head;
        ListNode pre = head;
        ListNode ppre = head;
        while (p != null) {
            p = p.next;
            n--;
            if (n < 0) {
                ppre = pre;
                pre = pre.next;
            }
        }
        if (n == 0) {
            return head.next;
        } else {
            ppre.next = pre.next;
        }

        return head;
    }

}
